using System;
using FireStarter.Base.BusinessObjectMerger;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;

namespace FireStarterModeller.Logic.MergeBusinessObjects
{
    // This classes are used automatically by the MergeableBORegistry
    // ReSharper disable UnusedMember.Global 
    public class MergeDMUniqueConstraint : MergeBO<DMUniqueConstraint>
    {
        protected override void MergeMultipleRelationship(IMultipleRelationship fromMultipleRelationship, IMultipleRelationship toMultipleRelationship)
        {
            if (toMultipleRelationship == null || fromMultipleRelationship == null) return;
            //This has to be done in the MergeMultiple Relationship and not the MergeBOCol because
            // this needs to get the DMProperty from the DMClass that is associated with the Unique Constraint.
            DMUniqueConstraint toUC = (DMUniqueConstraint) toMultipleRelationship.OwningBO;
            var fromBOCol = fromMultipleRelationship.BusinessObjectCollection;
            var toBoCol = toMultipleRelationship.BusinessObjectCollection;
            var uCProps = toUC.UniqueConstraintProperties;
            while (uCProps.Count > 0)
            {
                //Temp_: this will possibly be changed when we add delete tracking to the merging.
                uCProps.MarkForDelete(uCProps[0]);
            }
            foreach (DMUniqueConstraintProperty fromRelatedBO in fromBOCol)
            {
                var toUCProp = new DMUniqueConstraintProperty();
                if (toUC.Class != null)
                {
                    IDMProperty dmProperty = toUC.Class.GetDMProperty(fromRelatedBO.Property.PropertyName);
                    toUCProp.Property = (DMProperty) dmProperty;
                }
                toBoCol.Add(toUCProp);
            }
        }
    }
    // ReSharper restore UnusedMember.Global
}